package de.duester.genealogy.model;

import java.util.List;
import de.duester.genealogy.model.dao.DaoSession;
import de.greenrobot.dao.DaoException;

import de.duester.genealogy.model.dao.EventDao;
import de.duester.genealogy.model.dao.EventDateDao;
import de.duester.genealogy.model.dao.EventDocumentDao;
import de.duester.genealogy.model.dao.EventPlaceDao;
import de.duester.genealogy.model.dao.EventStringDao;
import de.duester.genealogy.model.dao.PersonEventDao;

// THIS CODE IS GENERATED BY greenDAO, EDIT ONLY INSIDE THE "KEEP"-SECTIONS

// KEEP INCLUDES - put your custom includes here
import de.duester.genealogy.model.service.EventService;
// KEEP INCLUDES END
/**
 * Entity mapped to table EVENT.
 */
public class Event extends GenealogicalObject  {
    private String typeCode;

    /** Used to resolve relations */
    private transient DaoSession daoSession;

    /** Used for active entity operations. */
    private transient EventDao myDao;

    private List<PersonEvent> personEvents;
    private List<EventDocument> eventDocuments;
    private List<EventPlace> eventPlaces;
    private List<EventDate> eventDates;
    private List<EventString> eventStrings;

    // KEEP FIELDS - put your custom fields here
    // KEEP FIELDS END

    public Event() {
    }

    public Event(Long id) {
        this.id = id;
    }

    public Event(Long id, String typeCode) {
        this.id = id;
        this.typeCode = typeCode;
    }

    /** called by internal mechanisms, do not call yourself. */
    public void __setDaoSession(DaoSession daoSession) {
        this.daoSession = daoSession;
        myDao = daoSession != null ? daoSession.getEventDao() : null;
    }

    public String getTypeCode() {
        return typeCode;
    }

    public void setTypeCode(String typeCode) {
        this.typeCode = typeCode;
    }

    /** To-many relationship, resolved on first access (and after reset). Changes to to-many relations are not persisted, make changes to the target entity. */
    public List<PersonEvent> getPersonEvents() {
        if (personEvents == null) {
            if (daoSession == null) {
                throw new DaoException("Entity is detached from DAO context");
            }
            PersonEventDao targetDao = daoSession.getPersonEventDao();
            List<PersonEvent> personEventsNew = targetDao._queryEvent_PersonEvents(id);
            synchronized (this) {
                if(personEvents == null) {
                    personEvents = personEventsNew;
                }
            }
        }
        return personEvents;
    }

    /** Resets a to-many relationship, making the next get call to query for a fresh result. */
    public synchronized void resetPersonEvents() {
        personEvents = null;
    }

    /** To-many relationship, resolved on first access (and after reset). Changes to to-many relations are not persisted, make changes to the target entity. */
    public List<EventDocument> getEventDocuments() {
        if (eventDocuments == null) {
            if (daoSession == null) {
                throw new DaoException("Entity is detached from DAO context");
            }
            EventDocumentDao targetDao = daoSession.getEventDocumentDao();
            List<EventDocument> eventDocumentsNew = targetDao._queryEvent_EventDocuments(id);
            synchronized (this) {
                if(eventDocuments == null) {
                    eventDocuments = eventDocumentsNew;
                }
            }
        }
        return eventDocuments;
    }

    /** Resets a to-many relationship, making the next get call to query for a fresh result. */
    public synchronized void resetEventDocuments() {
        eventDocuments = null;
    }

    /** To-many relationship, resolved on first access (and after reset). Changes to to-many relations are not persisted, make changes to the target entity. */
    public List<EventPlace> getEventPlaces() {
        if (eventPlaces == null) {
            if (daoSession == null) {
                throw new DaoException("Entity is detached from DAO context");
            }
            EventPlaceDao targetDao = daoSession.getEventPlaceDao();
            List<EventPlace> eventPlacesNew = targetDao._queryEvent_EventPlaces(id);
            synchronized (this) {
                if(eventPlaces == null) {
                    eventPlaces = eventPlacesNew;
                }
            }
        }
        return eventPlaces;
    }

    /** Resets a to-many relationship, making the next get call to query for a fresh result. */
    public synchronized void resetEventPlaces() {
        eventPlaces = null;
    }

    /** To-many relationship, resolved on first access (and after reset). Changes to to-many relations are not persisted, make changes to the target entity. */
    public List<EventDate> getEventDates() {
        if (eventDates == null) {
            if (daoSession == null) {
                throw new DaoException("Entity is detached from DAO context");
            }
            EventDateDao targetDao = daoSession.getEventDateDao();
            List<EventDate> eventDatesNew = targetDao._queryEvent_EventDates(id);
            synchronized (this) {
                if(eventDates == null) {
                    eventDates = eventDatesNew;
                }
            }
        }
        return eventDates;
    }

    /** Resets a to-many relationship, making the next get call to query for a fresh result. */
    public synchronized void resetEventDates() {
        eventDates = null;
    }

    /** To-many relationship, resolved on first access (and after reset). Changes to to-many relations are not persisted, make changes to the target entity. */
    public List<EventString> getEventStrings() {
        if (eventStrings == null) {
            if (daoSession == null) {
                throw new DaoException("Entity is detached from DAO context");
            }
            EventStringDao targetDao = daoSession.getEventStringDao();
            List<EventString> eventStringsNew = targetDao._queryEvent_EventStrings(id);
            synchronized (this) {
                if(eventStrings == null) {
                    eventStrings = eventStringsNew;
                }
            }
        }
        return eventStrings;
    }

    /** Resets a to-many relationship, making the next get call to query for a fresh result. */
    public synchronized void resetEventStrings() {
        eventStrings = null;
    }

    /** Convenient call for {@link AbstractDao#delete(Object)}. Entity must attached to an entity context. */
    public void delete() {
        if (myDao == null) {
            throw new DaoException("Entity is detached from DAO context");
        }    
        myDao.delete(this);
    }

    /** Convenient call for {@link AbstractDao#update(Object)}. Entity must attached to an entity context. */
    public void update() {
        if (myDao == null) {
            throw new DaoException("Entity is detached from DAO context");
        }    
        myDao.update(this);
    }

    /** Convenient call for {@link AbstractDao#refresh(Object)}. Entity must attached to an entity context. */
    public void refresh() {
        if (myDao == null) {
            throw new DaoException("Entity is detached from DAO context");
        }    
        myDao.refresh(this);
    }

    // KEEP METHODS - put your custom methods here
	@Override
	public boolean equalsByContent(Object o) {
		Event e = (Event) o;
		return (typeCode.equals(e.typeCode));
	}

	@Override
	public String toString() {
		// TODO toString
		return super.toString();
	}

	// Vergleich nach Datum (Rolle "date")
	public int compare(Event event) {
		Date date1 = EventService.getDate(this, "date");
		Date date2 = EventService.getDate(event, "date");

		if (date1 == null && date2 == null)
			return 0;
		if (date1 == null)
			return 1;
		if (date2 == null)
			return -1;
		return date1.compare(date2);
	}
    // KEEP METHODS END

}
